/*
 * TestDumpCCDeclarator.c
 *
 *  Created on: 2012/05/31
 *      Author: guanxiaoying
 */
#include "TestDump.h"
static VOID dumpDeclarator( AST astDeclarator ){
	AST astDeclaration = CDeclarator.getDeclaration( astDeclarator);
	AST astCodeScp = CDeclarator.getCodeScope( astDeclarator );
	S2 s2Cnt;

	sprintf( tempBuf, header_format, dumpGetStr(XML_RECORD), astDeclarator );
	dumpOutputStream( tempBuf );
	dumpCodeScopeAsField( astCodeScp );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_DECLARATION),  "", astDeclaration );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_DECLARATION) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_DECLARATOR),  "next" ,
			CDeclarator.getNextDeclarator( astDeclarator));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_DECLARATOR) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_DECLARATOR),  "child" ,
			CDeclarator.getChildDeclarator( astDeclarator));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_DECLARATOR) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_DECLARATOR),  "parent" ,
			CDeclarator.getParentDeclarator( astDeclarator));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_DECLARATOR) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_TYPE),  "" ,
			CDeclarator.getType( astDeclarator));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_TYPE) );
	dumpOutputStream( tempBuf );

	if( CDeclarator.isPointerDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "pointer" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );

		s2Cnt = CDeclarator.pointer_getConstNum( astDeclarator );
		while( s2Cnt > 0 ){
			sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ),"" );
			dumpOutputStream( tempBuf );
			dumpParameterStr( "const" );
			sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG) );
			dumpOutputStream( tempBuf );
			s2Cnt--;
		}
		s2Cnt = CDeclarator.pointer_getVolatileNum( astDeclarator );
		while( s2Cnt > 0 ){
			sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ),"" );
			dumpOutputStream( tempBuf );
			dumpParameterStr( "volatile" );
			sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG) );
			dumpOutputStream( tempBuf );
			s2Cnt--;
		}
		s2Cnt = CDeclarator.pointer_getRestrictNum( astDeclarator );
		while( s2Cnt > 0 ){
			sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ),"" );
			dumpOutputStream( tempBuf );
			dumpParameterStr( "restrict" );
			sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG) );
			dumpOutputStream( tempBuf );
			s2Cnt--;
		}
	}else if( CDeclarator.isParentheseDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "parentheses" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}else if( CDeclarator.isSymbolDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "identifier" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_SYMBOL ),
				"", CDeclarator.symbol_getSymbol( astDeclarator ) );
		dumpOutputStream( tempBuf );
		dumpParameterStr( CSymbol.getName( CDeclarator.symbol_getSymbol( astDeclarator ) ) );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_SYMBOL ) );
		dumpOutputStream( tempBuf );
	}else if( CDeclarator.isFunctionDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "function" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_DECLARATION ),
				"", CDeclarator.function_getParamChain( astDeclarator ) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_DECLARATION ) );
		dumpOutputStream( tempBuf );
	}else if( CDeclarator.isArrayDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "array" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_EXPRESSION ),
				"", CDeclarator.array_getArrayIndex( astDeclarator ) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_EXPRESSION ) );
		dumpOutputStream( tempBuf );
	}else if( CDeclarator.isBitfieldDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "bit-field" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_EXPRESSION ),
				"", CDeclarator.array_getArrayIndex( astDeclarator ) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_EXPRESSION ) );
		dumpOutputStream( tempBuf );
	}
	else if( CDeclarator.isInitialExpDeclarator( astDeclarator )){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "initializer" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );

		sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_INITIALIZER ),
				"", CDeclarator.initExp_getInitExp( astDeclarator ) );
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_INITIALIZER ) );
		dumpOutputStream( tempBuf );
	}else{
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND ),
				"" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( dumpGetStr( XML_ERROR ) );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}

	sprintf( tempBuf, tail_format, dumpGetStr(XML_RECORD) );
	dumpOutputStream( tempBuf );
}
VOID DumpDeclarator(  PSZ szPath, PSZ postfix ){
	AST astDeclarator;
	AST astMax;

	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS == dumpOutputStreamInit_file(file_buf)){
		astDeclarator = MIN_ARRAY;
		astMax = CDeclarator.getMax();
		sprintf( tempBuf, top_header, dumpGetStr(XML_DECLARATOR) );
		dumpOutputStream( tempBuf );
		while( astDeclarator <= astMax ){
			dumpDeclarator( astDeclarator );
			astDeclarator ++;
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_DECLARATOR) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}
